package com.panopset.gui;

import static com.panopset.Strings.isPopulated;

import java.awt.FlowLayout;
import java.io.StringWriter;
import java.util.Calendar;

import javax.swing.JPanel;
import javax.swing.JPasswordField;

import com.panopset.Strings;
import com.panopset.centralen.UniqueKey;
import com.panopset.gui.TypicalDialog.DIALOG_FLAVOR;
import com.panopset.swing.App;
import com.panopset.swing.Field;
import com.panopset.swing.Scaffolding;

/**
 * Widgets.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Widgets {


    /**
     * Number of columns for the password.
     */
    private static final int COLUMNS = 20;

    /**
     * Prompt for password.
     *
     * @return password.
     */
    public static String promptForPassword() {
        if (App.app == null) {
            Scaffolding.s.setAppName("pwd");
            Scaffolding.s.load();
        }
        String key = UniqueKey.generateStackHashKey();
        String rtn = "";
        JPasswordField jpw = new JPasswordField();
        JPanel p = new JPanel(new FlowLayout());
        p.add(jpw);
        TypicalDialog d = new TypicalDialog.Builder(
                "Please enter your password").centerPanel(p).remember()
                .dialogFlavor(DIALOG_FLAVOR.OK_CANCEL)
                .construct();
        d.getFrame();

        if (d.getRememberCheckbox().getValue().equalsIgnoreCase("true")) {
            String pwd = Scaffolding.s.get(key);
            if (isPopulated(pwd)) {
                return pwd;
            }
        }
        d.getPanel().setLayout(new FlowLayout());
        jpw.setColumns(COLUMNS);
        d.getFrame().setVisible(true);
        char[] pwa = jpw.getPassword();
        if (pwa != null) {
            rtn = new String(jpw.getPassword());
        }
        Field.synch();
        if (d.isRememberChecked()) {
            Scaffolding.s.put(key, rtn);
        } else {
            Scaffolding.s.put(key, "");
        }
        Scaffolding.s.flush();
        if (d.isUserCancelled()) {
            rtn = "";
        }
        return rtn;
    }
	/**
	 * @param trialYear Year of trial use.
	 * @return False if over a year has passed, with warnings leading up to it.
	 */
	public static boolean isLicenseExpired(int trialYear) {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int yr = trialYear + 1;

		if (year > yr) {
			StringWriter sw = new StringWriter();
			sw = new StringWriter();
			sw.append("License has expired.");
			sw.append(Strings.getEol());
			sw.append(Strings.getEol());
			sw
					.append("It is now after " + yr + ", and Panopset contract terms were not met.");
			sw.append(Strings.getEol());
			sw.append("It's been over a year, shutting down.");
			TypicalDialog.dspdlg("License Terminated", sw.toString());
			return true;
		}

		if (year > trialYear) {
			StringWriter sw = new StringWriter();
			sw.append("License has expired.");
			sw.append(Strings.getEol());
			sw.append(Strings.getEol());
			sw
					.append("It is after " + trialYear + ", and Panopset contract terms were not met.");
			sw.append(Strings.getEol());
			sw
					.append("Continued use of this program, with no intent to pay, is... disappointing.");
			TypicalDialog.dspdlg("License Warning", sw.toString());
		}
		return false;
	}


    /**
     * Prevent instantiation.
     */
    private Widgets() { }
}
